﻿using DataAchive.Model.Excel.Export;
using NPOI.SS.Formula.Functions;
using SoftCommon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DataAchive.Extend
{
    //主分类不知 且每个机台配置会有差别，
  


    public class AiDefectStaticRRR
    {
        public static List<AiDefectStaticColumnAiC> GetHReportDataAi(List<ColRowReport> heBingReport)
        {
            var ProductCount = heBingReport.Count;
            var OKCount = heBingReport.Count(x => x.HumanJudgeTime == TimeSpan.Zero);
            var NGCount = ProductCount - OKCount;
            Dictionary<string,int>countDictionary = new Dictionary<string,int>();
            foreach (var itemCol in heBingReport)
            {
                var item_el = itemCol.ElAiDetectInfo.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var item_vi = itemCol.ViAiDetectInfo.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var item_vB = itemCol.VBAiDetectInfo.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var elFault in item_el)
                {
                    var fault = elFault.Trim();
                    if (countDictionary.ContainsKey(fault))
                    {
                        countDictionary[fault] ++;
                    }
                    else
                    {
                        countDictionary[fault] = 1;
                    }
                }
                foreach (var viFault in item_vi)
                {
                    var fault = viFault.Trim();
                    if (countDictionary.ContainsKey(fault))
                    {
                        countDictionary[fault]++;
                    }
                    else
                    {
                        countDictionary[fault] = 1;
                    }
                }
                foreach (var vbFault in item_vB)
                {
                    var fault = vbFault.Trim();
                    if (countDictionary.ContainsKey(fault))
                    {
                        countDictionary[fault]++;
                    }
                    else
                    {
                        countDictionary[fault] = 1;
                    }
                }
            }

            List<AiDefectStaticColumnAiC> aiCs = new List<AiDefectStaticColumnAiC>();
            foreach (var everyOne in countDictionary)
            {
                string keyN = everyOne.Key;
                if (everyOne.Key.Length < 2)
                {
                    SoftCommon.Log.Write("special default ignore:"+ everyOne.Key);
                    continue;
                }
                if (everyOne.Key.Equals("AI", StringComparison.OrdinalIgnoreCase))
                {
                    keyN = "AiFaild";
                }
                AiDefectStaticColumnAiC aiDefect = new AiDefectStaticColumnAiC();
                aiDefect.DefectName = keyN;
                aiDefect.DefectCount =everyOne.Value;

                //total
                aiDefect.ProductCount =ProductCount;
                aiDefect.OKCount =OKCount;
                aiDefect.NGCount =NGCount;

                aiCs.Add(aiDefect);
            }
            return aiCs;
        }

        public static List<AiDefectStaticColumnHumanC> GetHReportDataHuman(List<ColRowReport> heBingReport)
        {
            //误检数据=>过判的el和ai的检测数据 VB?
            var loujianWupan = heBingReport.Where(x => x.LouJianOrGuopan != null && x.LouJianOrGuopan.Equals("过判"));
            var ProductCount = heBingReport.Count;
            var OKCount = heBingReport.Count(x => x.HumanJudgeTime == TimeSpan.Zero);
            var NGCount = ProductCount - OKCount;
            Dictionary<string, int> countDictionary = new Dictionary<string, int>();
            foreach (var itemCol in loujianWupan)
            {
                var item_el = itemCol.ElAiDetectInfo.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var item_vi = itemCol.ViAiDetectInfo.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var item_vB = itemCol.VBAiDetectInfo.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var elFault in item_el)
                {
                    var fault = elFault.Trim();
                    if (countDictionary.ContainsKey(fault))
                    {
                        countDictionary[fault]++;
                    }
                    else
                    {
                        countDictionary[fault] = 1;
                    }
                }
                foreach (var viFault in item_vi)
                {
                    var fault = viFault.Trim();
                    if (countDictionary.ContainsKey(fault))
                    {
                        countDictionary[fault]++;
                    }
                    else
                    {
                        countDictionary[fault] = 1;
                    }
                }
                foreach (var vbFault in item_vB)
                {
                    var fault = vbFault.Trim();
                    if (countDictionary.ContainsKey(fault))
                    {
                        countDictionary[fault]++;
                    }
                    else
                    {
                        countDictionary[fault] = 1;
                    }
                }
            }

            List<AiDefectStaticColumnHumanC> humanCs = new List<AiDefectStaticColumnHumanC>();
            foreach (var everyOne in countDictionary)
            {
                AiDefectStaticColumnHumanC aiDefect = new AiDefectStaticColumnHumanC();

                string keyN = everyOne.Key;
                if (everyOne.Key.Length < 2)
                {
                    SoftCommon.Log.Write("special default ignore:" + everyOne.Key);
                    continue;
                }
                if (everyOne.Key.Equals("AI", StringComparison.OrdinalIgnoreCase))
                {
                    keyN = "AiFaild";
                }

                aiDefect.DefectName = keyN;
                aiDefect.DefectCount = everyOne.Value;

                //total
                aiDefect.ProductCount = ProductCount;
                aiDefect.OKCount = OKCount;
                aiDefect.NGCount = NGCount;

                humanCs.Add(aiDefect);
            }
            return humanCs;
        }



        public static List<AiDefectStaticColumnAiC> GetHReportDataAi11111(List<ColRowReport> heBingReport)
        {
            var ProductCount = heBingReport.Count;
            var OKCount = heBingReport.Count(x => x.HumanJudgeTime == TimeSpan.Zero);
            var NGCount = ProductCount - OKCount;
            Dictionary<string, int> countDictionary = new Dictionary<string, int>();
            foreach (var itemCol in heBingReport)
            {
                var item_el = itemCol.ElAiDetectInfo.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var item_vi = itemCol.ViAiDetectInfo.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var item_vB = itemCol.VBAiDetectInfo.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var elFault in item_el)
                {
                    var fault = elFault.Trim();
                    if (IsNumeric(fault))
                    {
                        continue;
                    }
                    if (countDictionary.ContainsKey(fault))
                    {
                        countDictionary[fault]++;
                    }
                    else
                    {
                        countDictionary[fault] = 1;
                    }
                }
                foreach (var viFault in item_vi)
                {
                    var fault = viFault.Trim();
                    if (IsNumeric(fault))
                    {
                        continue;
                    }
                    if (countDictionary.ContainsKey(fault))
                    {
                        countDictionary[fault]++;
                    }
                    else
                    {
                        countDictionary[fault] = 1;
                    }
                }
                foreach (var vbFault in item_vB)
                {
                    var fault = vbFault.Trim();
                    if (IsNumeric(fault))
                    {
                        continue;
                    }
                    if (countDictionary.ContainsKey(fault))
                    {
                        countDictionary[fault]++;
                    }
                    else
                    {
                        countDictionary[fault] = 1;
                    }
                }
            }

            List<AiDefectStaticColumnAiC> aiCs = new List<AiDefectStaticColumnAiC>();
            foreach (var everyOne in countDictionary)
            {
                string keyN = everyOne.Key;
                if (everyOne.Key.Length < 2)
                {
                    SoftCommon.Log.Write("special default ignore:" + everyOne.Key);
                    continue;
                }
                if (everyOne.Key.Equals("AI", StringComparison.OrdinalIgnoreCase))
                {
                    keyN = "AiFaild";
                }
                AiDefectStaticColumnAiC aiDefect = new AiDefectStaticColumnAiC();
                aiDefect.DefectName = keyN;
                aiDefect.DefectCount = everyOne.Value;

                //total
                aiDefect.ProductCount = ProductCount;
                aiDefect.OKCount = OKCount;
                aiDefect.NGCount = NGCount;

                aiCs.Add(aiDefect);
            }
            return aiCs;
        }
        /// <summary>
        /// 判断字符串是否为有效的数字（整数或小数）
        /// </summary>
        /// <param name="value">要检查的字符串</param>
        /// <returns>如果是数字则返回true，否则返回false</returns>
       public static bool IsNumeric(string value)
        {
            // 尝试将字符串转换为double类型
            return double.TryParse(value, out _);
        }
    }
}
